package com.share.lottery.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.share.lottery.R;
import com.share.lottery.activity.caibotools.SeachGridFragment;
import com.share.lottery.activity.fans.SelectPhotoActiviy;
import com.share.lottery.activity.personal.PersonFrgment;
import com.share.lottery.adapter.MyFragmentPagerAdapter;
import com.share.lottery.fragment.HomeFragment;

/**
 * @author py 影音棚
 * 
 */
public class JcMainActivtiy extends FragmentActivity implements
		OnClickListener {
	private ImageView tv0;
	private ImageView tv1;
	private ImageView tv2;
	private ImageView tv3;


	public ViewPager mPager;
	private ImageView image;
	private int bmpW;
	private ArrayList<Fragment> fragmentList;
	private RelativeLayout layout0;
	private RelativeLayout layout1;
	private RelativeLayout layout2;
	private RelativeLayout layout3;
	private ImageView tab_slider;
	private int offset;

	/**
	 * 业务的fragment
	 */
	private HomeFragment homeFragment;
	private SeachGridFragment t1;
	private PersonFrgment t2;
	private HomeFragment t3;
	private int currIndex = 0;

	private ImageView imgTake;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.jc_main);
		init();

		

	}

	protected void onDestroy() {

		super.onDestroy();
	}

	private void init() {
		findView();

		setData();
		InitViewPager();
		setOnCliker();

	}

	private void setOnCliker() {
		imgTake.setOnClickListener(this);
	}

	private void setData() {


	}

	private void findView() {	
		imgTake = (ImageView) findViewById(R.id.img_take);
		InitItem_bg();

	}

	public int getScreenWidth1() {
		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

		int width = wm.getDefaultDisplay().getWidth();// 屏幕宽度
		return width;
	}

	/**
	 * 初始化tab滑动背景
	 */
	private void InitItem_bg() {

		tab_slider = (ImageView) findViewById(R.id.tab_main_slider);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.myvideos_topselection);
		offset = (getScreenWidth1()/2/4);

		float scale_w = (float) offset / bitmap.getWidth();
		Matrix matrix = new Matrix();
		matrix.postScale(scale_w, 0.85f);
		tab_slider.setImageMatrix(matrix);// 控制iteam_bg在不同的屏自适应

		layout0 = (RelativeLayout) findViewById(R.id.destn_select0);
		layout1 = (RelativeLayout) findViewById(R.id.destn_select1);
		layout2 = (RelativeLayout) findViewById(R.id.destn_select2);
		layout3 = (RelativeLayout) findViewById(R.id.destn_select3);
		// layout0.setOnClickListener(changeListener);
		// layout1.setOnClickListener(changeListener);
		// layout2.setOnClickListener(changeListener);

		layout0.setOnClickListener(new txListener(0));
		layout1.setOnClickListener(new txListener(1));
		layout2.setOnClickListener(new txListener(2));
		layout3.setOnClickListener(new txListener(3));
		tv0 = (ImageView) findViewById(R.id.tv0);
		tv1 = (ImageView) findViewById(R.id.tv1);
		tv2 = (ImageView) findViewById(R.id.tv2);
		tv3 = (ImageView) findViewById(R.id.tv3);

	}

	/********************************************************************************************/
	public class txListener implements View.OnClickListener {
		private int index = 0;

		public txListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mPager.setCurrentItem(index);
		}
	}

	/*
	 * 初始化ViewPager
	 */

	public void InitViewPager() {


//		tv1.setText("音频");
//		tv0.setText("视频");
//		tv2.setText("照片");
		image = (ImageView) findViewById(R.id.tab_main_slider);
		mPager = (ViewPager) findViewById(R.id.viewpager);
		fragmentList = new ArrayList<Fragment>();
		if (homeFragment == null) {
			homeFragment = new HomeFragment();
		}
		if (t1 == null) {
			t1 = new SeachGridFragment();
		}
		if (t2 == null) {
			t2 = new PersonFrgment();
		}
		if (t3 == null) {
			t3 = new HomeFragment();
		}
		fragmentList.add(t1);
		fragmentList.add(homeFragment);
		fragmentList.add(t2);
		fragmentList.add(t3);
		
		// 给ViewPager设置适配器
		mPager.setAdapter(new MyFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentList));
		mPager.setCurrentItem(0);// 设置当前显示标签页为第一页
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器

		mPager.setOffscreenPageLimit(2);

		switch (currIndex) {
		case 0:
			layout0.performClick();
			break;
		case 1:
			layout1.performClick();
			break;
		case 2:
			layout2.performClick();
			break;
		case 3:
			layout3.performClick();
			break;
		default:
			break;
		}
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {
		private int one = offset + bmpW;// 两个相邻页面的偏移量

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			Animation animation = new TranslateAnimation(currIndex * one, arg0
					* one, 0, 0);// 平移动画
			currIndex = arg0;
			animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
			animation.setDuration(200);// 动画持续时间0.2秒
			image.startAnimation(animation);// 是用ImageView来显示动画的

//			switch (arg0) {
//			case 0:
//				tv0.setTextColor(getResources().getColor(R.color.TextColorBlue));
//				tv1.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				tv2.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				tv3.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				break;
//			case 1:
//				tv1.setTextColor(getResources().getColor(R.color.TextColorBlue));
//				tv0.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				tv2.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				tv3.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				break;
//			case 2:
//				tv2.setTextColor(getResources().getColor(R.color.TextColorBlue));
//				tv0.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				tv1.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				tv3.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				break;
//				
//			case 3:
//				tv3.setTextColor(getResources().getColor(R.color.TextColorBlack));
//				tv2.setTextColor(getResources().getColor(R.color.TextColorBlack));
//				tv0.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				tv1.setTextColor(getResources()
//						.getColor(R.color.TextColorBlack));
//				break;
//
//			default:
//				break;
//			}
		}
	}

	@Override
	protected void onRestart() {

		super.onRestart();
		init();
	}


	public static int current = 0;

	
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.img_take://拍照或相册
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), SelectPhotoActiviy.class);
			startActivity(intent);
		break;
		
		case R.id.title_right_tv:// 添加
	

			switch (currIndex) {
			case 0:// 我的视频

				break;
			case 1:// 我的音频

				break;
			case 2:// 我的图片

				break;

			default:
				break;
			}

			break;

		default:
			break;
		}

	}
	long lastbackclick = 0;
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if (System.currentTimeMillis() - lastbackclick > 2000) {
				Toast.makeText(JcMainActivtiy.this, "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
				lastbackclick = System.currentTimeMillis();
			}

			else {
				moveTaskToBack(true);

			}
			return true;
		}

		return super.onKeyDown(keyCode, event);

	}
}
